﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

//using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace Hugo
{
    public class NotificationUIPage : UIPage
    {
        List<UIOption> pages;
        Dictionary<String, Rectangle> pageNameToSymbolMap;

        private static Action EmptyDelegate = delegate() { };

        double canvasTop;
        double iconY;

        Label notificationTitleLabel;

        const double OPACITY_UNSELECTED = 0.15;
        const double OPACITY_SELECTED = 1;

        Canvas IconCanvas;

        const int HOR_PADDING = 150;
        const int VER_BOTTOM_PADDING = 80;

        public NotificationUIPage(String name, String message, List<UIOption> options)
            : base(name, null)
        {
            
            IconCanvas = new Canvas();

            IconCanvas.Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - HOR_PADDING * 2;
            IconCanvas.Height = 350;// System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - canvasTop - VER_BOTTOM_PADDING;
            
            canvasTop = 200;//System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 2 - ICON_HEIGHT_SELECTED / 2 - 290;
            iconY = canvasTop;// System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - 60 - ICON_HEIGHT_SELECTED - canvasTop;

            Console.WriteLine(IconCanvas.Width + " x " + IconCanvas.Height);

            Canvas.SetLeft(IconCanvas, HOR_PADDING);
            Canvas.SetTop(IconCanvas, this.canvasTop);

            IconCanvas.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.DarkSlateGray);
            IconCanvas.Opacity = 0.95;
            IconCanvas.Name = name.Replace(" ","") + "_iconcanvas" + (nextCanvasID++);
            MainWindow.RegisterName(IconCanvas.Name, IconCanvas);
            this.PageCanvas.Children.Add(IconCanvas);


            pages = new List<UIOption>();
            pageNameToSymbolMap = new Dictionary<String, Rectangle>();

            foreach (UIOption p in options) {
                addUIOption(p);
            }

            notificationTitleLabel = new Label();
            notificationTitleLabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            notificationTitleLabel.Content = this.Name;
            notificationTitleLabel.Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.White);
            notificationTitleLabel.FontSize = 60;
            notificationTitleLabel.FontWeight = FontWeights.Bold;
            IconCanvas.Children.Add(notificationTitleLabel);
            Canvas.SetTop(notificationTitleLabel, 15);
            // need to rerender to get updated width
            notificationTitleLabel.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
            Canvas.SetLeft(notificationTitleLabel, 20);//IconCanvas.Width / 2 - notificationTitleLabel.ActualWidth / 2);

            
            TextBox notificationMessageTextBox = new TextBox();
            notificationMessageTextBox.Width = IconCanvas.Width - HOR_PADDING;
            notificationMessageTextBox.Text = message;
            notificationMessageTextBox.TextWrapping = TextWrapping.WrapWithOverflow;
            notificationMessageTextBox.FontSize = 40;
            notificationMessageTextBox.BorderBrush = null;
            notificationMessageTextBox.Foreground = System.Windows.Media.Brushes.White;
                //new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.White);
            notificationMessageTextBox.Background = System.Windows.Media.Brushes.DarkSlateGray;

            IconCanvas.Children.Add(notificationMessageTextBox);
            Canvas.SetTop(notificationMessageTextBox, 120);
            // need to rerender to get updated width
            notificationMessageTextBox.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
            Canvas.SetLeft(notificationMessageTextBox, 20);// IconCanvas.Width / 2 - notificationMessageTextBox.ActualWidth / 2);
            
        }

        private void setTransparent(Rectangle r)
        {
            r.Opacity = 0;
        }

        private void setOpaque(Rectangle r)
        {
            r.Opacity = 1;
        }

        static int nextCanvasID = 0;

        static int counter = 0;
        int currentpos = 0;

        public void addUIOption(UIOption page)
        {
            pages.Add(page);

            Rectangle rect = getSymbol(page.IconSource);

            pageNameToSymbolMap.Add(page.Name, rect);

            setTransparent(rect);

            Regex rgx = new Regex("[^a-zA-Z0-9]");

            rect.Name = "notificationuipage" + (counter++) + rgx.Replace(page.Name, "") + pages.Count + "_rect";
            this.MainWindow.RegisterName(rect.Name, rect);

            IconCanvas.Children.Add(rect);
            currentpos = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 2 - (pages.Count * ICON_WIDTH_SELECTED + (pages.Count - 1) * ICON_BUFFER) / 2;
            //Canvas.SetLeft(IconCanvas, currentpos);

            Point p = getCenterOfRectangleAtIndex(pages.Count - 1);
            Canvas.SetLeft(rect, p.X);
            Canvas.SetTop(rect, p.Y);

            this.resizeOnCenter(rect, ICON_WIDTH_UNSELECTED, ICON_HEIGHT_UNSELECTED);

            rect.Opacity = OPACITY_UNSELECTED;
        }

        private Point getCenterOfRectangleAtIndex(int index) {
            double x = index * ICON_WIDTH_SELECTED + (index+1)*ICON_BUFFER;// (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 2) - ICON_WIDTH_SELECTED / 2 + (ICON_WIDTH_SELECTED * index) + index * ICON_BUFFER;
            double y = this.iconY;
            return new Point(x, y);
        }

        private Rectangle getRectangleAt(int index)
        {
            return pageNameToSymbolMap[pages[index].Name];
        }

        private void resizeOnCenter(Rectangle r, int newWidth, int newHeight)
        {
            DoubleAnimation widthAnimation = AnimationFactory.getScaleAnimation(r.ActualWidth, newWidth);
            DoubleAnimation heightAnimation = AnimationFactory.getScaleAnimation(r.ActualHeight, newHeight);

            double oldCenterX = Canvas.GetLeft(r) + r.ActualWidth / 2;
            double oldCenterY = Canvas.GetTop(r) + r.ActualHeight / 2;
            double newCenterX = Canvas.GetLeft(r) + newWidth / 2;
            double newCenterY = Canvas.GetTop(r) + newHeight / 2;

            DoubleAnimation xAnimation = AnimationFactory.getScaleAnimation(oldCenterX, newCenterX);
            DoubleAnimation yAnimation = AnimationFactory.getScaleAnimation(oldCenterY, newCenterY);

            Storyboard widthStoryBoard = new Storyboard();
            widthStoryBoard.Children.Add(widthAnimation);
            Storyboard.SetTargetName(widthAnimation, r.Name);
            Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(Rectangle.WidthProperty));
            widthStoryBoard.Begin(this.MainWindow);

            Storyboard heightStoryboard = new Storyboard();
            heightStoryboard.Children.Add(heightAnimation);
            Storyboard.SetTargetName(heightAnimation, r.Name);
            Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Rectangle.HeightProperty));
            heightStoryboard.Begin(this.MainWindow);



            /*
            double oldCenterX = Canvas.GetLeft(r) + r.Width / 2;
            double oldCenterY = Canvas.GetTop(r) + r.Height / 2;

            r.Width = newWidth;
            r.Height = newHeight;

            Canvas.SetLeft(r, oldCenterX - r.Width / 2);
            Canvas.SetTop(r, oldCenterY - r.Height / 2);
             */
        }

        public override void setCursorPosition(Point p)
        { }
        
        /*
        private Rectangle getSymbol(BitmapSource u)
        {
            Rectangle item = new Rectangle();
            item.Height = ICON_HEIGHT_SELECTED;
            item.Width = ICON_WIDTH_SELECTED;
            System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush();
            myBrush.ImageSource = u;
            item.Fill = myBrush;

            return item;
        }
         */

        public List<UIOption> getUIPages()
        {
            return this.pages;
        }

        public override void respondToKey(KeyEventArgs e)
        {
            if (e.Key == Key.Left)
            {
                //shiftLeft();
            }
            else if (e.Key == Key.Right)
            {
                //shiftRight();
            }
            else if (e.Key == Key.Enter)
            {
                //UIPage selectedPage = getSelectedPage();
                //MainWindow.pushPage(selectedPage);
            }
            else if (e.Key == Key.Back)
            {
                MainWindow.popPage();
            }
        }

        public override void setPage()
        {
            //System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height -

            double from = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - this.canvasTop;// PageCanvas.ActualHeight;
            double amount = -from;// -PageCanvas.ActualHeight;

            Canvas.SetTop(PageCanvas, from);
            PageCanvas.Opacity = 1;

            DoubleAnimation myDoubleAnimation = AnimationFactory.getShiftAnimation(from, amount,500);

            Storyboard myStoryboard = new Storyboard();
            myStoryboard.Children.Add(myDoubleAnimation);
            Storyboard.SetTargetName(myDoubleAnimation, PageCanvas.Name);
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Canvas.TopProperty));

            myStoryboard.Begin(this.MainWindow);
            
        }

        public override void unSetPage()
        {
            //System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height -

            double from = 0;// PageCanvas.ActualHeight;
            double amount = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - this.canvasTop;// -PageCanvas.ActualHeight;

            Canvas.SetTop(PageCanvas, from);
            PageCanvas.Opacity = 1;

            DoubleAnimation myDoubleAnimation = AnimationFactory.getShiftAnimation(from, amount, 500);

            Storyboard myStoryboard = new Storyboard();
            myStoryboard.Children.Add(myDoubleAnimation);
            Storyboard.SetTargetName(myDoubleAnimation, PageCanvas.Name);
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Canvas.TopProperty));

            myStoryboard.Begin(this.MainWindow);

        }
    }
}
